package postgres

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"manager/errors"
	"manager/model"
	"manager/model/entity"
)

var UserBikeSetting = &userBikeSetting{}

type userBikeSetting struct{}

// Create 创建
func (a *userBikeSetting) Create(ctx context.Context, m *entity.UserBikeSetting) (int64, error) {
	err := GetDB(ctx).Create(m).Error
	return m.Id, err
}

// Find 查找详情
func (a *userBikeSetting) Find(ctx context.Context, in *model.UserBikeSettingInfoRequest) (*entity.UserBikeSetting, error) {
	e := &entity.UserBikeSetting{}

	q := GetDB(ctx).Model(&entity.UserBikeSetting{})

	if in.Id > 0 {
		err := q.First(&e, in.Id).Error
		return e, err
	}

	count := 0

	if in.Id > 0 {
		q = q.Where("id = ?", in.Id)
		count++
	}

	if in.UserId != nil {
		q = q.Where("user_id = ?", in.UserId)
		count++
	}

	if in.EbikeId != nil {
		q = q.Where("ebike_id = ?", in.EbikeId)
		count++
	}

	if in.EbikeImage != nil {
		q = q.Where("ebike_image = ?", in.EbikeImage)
	}

	if in.EbikeName != nil {
		q = q.Where("ebike_name = ?", in.EbikeName)
		count++
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
		count++
	}

	if count == 0 {
		return e, errors.New("condition illegal")
	}

	err := q.Order("updated_at desc").First(&e).Error
	return e, err
}

// Update 更新
func (a *userBikeSetting) Update(ctx context.Context, id int64, dict map[string]interface{}) error {
	if id > 0 {
		return GetDB(ctx).Model(&entity.UserBikeSetting{}).Where("id = ?", id).Updates(dict).Error
	}

	var userId, bikeId interface{}
	var ok bool
	if bikeId, ok = dict["ebike_id"]; !ok {
		return fmt.Errorf("ebike_id is need")
	}

	if userId, ok = dict["user_id"]; !ok {
		return fmt.Errorf("userid is need")
	}

	return GetDB(ctx).Model(&entity.UserBikeSetting{}).Where("ebike_id = ? and user_id = ?", bikeId, userId).Updates(dict).Error
}

// Delete 删除
func (a *userBikeSetting) Delete(ctx context.Context, id int64) error {
	return GetDB(ctx).Delete(&entity.UserBikeSetting{}, id).Error
}

func (a *userBikeSetting) GetUserBikeSettings(ctx context.Context, userId int64) ([]*entity.UserBikeSetting, error) {
	var userBikeSettings []*entity.UserBikeSetting
	err := GetDB(ctx).Model(&entity.UserBikeSetting{}).Where("user_id = ?", userId).Find(&userBikeSettings).Error
	return userBikeSettings, err
}

// List 列表查询
func (a *userBikeSetting) List(ctx context.Context, in *model.UserBikeSettingListRequest) (int, []*entity.UserBikeSetting, error) {
	var (
		q                = GetDB(ctx).Model(&entity.UserBikeSetting{})
		err              error
		total            int64
		userBikeSettings []*entity.UserBikeSetting
	)

	if in.Id > 0 {
		q = q.Where("id = ?", in.Id)
	}

	if in.UserId != nil {
		q = q.Where("user_id = ?", in.UserId)
	}

	if in.EbikeId != nil {
		q = q.Where("ebike_id = ?", in.EbikeId)
	}

	if len(in.BikeIds) > 0 {
		q = q.Where("ebike_id in (?)", in.BikeIds)
	}

	if in.EbikeImage != nil {
		q = q.Where("ebike_image = ?", in.EbikeImage)
	}

	if in.EbikeName != nil {
		q = q.Where("ebike_name = ?", in.EbikeName)
	}

	if in.UpdatedAt != nil {
		q = q.Where("updated_at = ?", in.UpdatedAt)
	}

	if err = q.Count(&total).Error; err != nil {
		return 0, nil, err
	}
	if err = q.Order("created_at desc").Limit(in.Size).Offset((in.Index - 1) * in.Size).Find(&userBikeSettings).Error; err != nil {
		return 0, nil, err
	}
	return int(total), userBikeSettings, nil
}

// ExecTransaction db事务执行
func (a *userBikeSetting) ExecTransaction(ctx context.Context, callback func(ctx context.Context) error) error {
	return GetDB(ctx).Transaction(func(tx *gorm.DB) error {
		ctx = context.WithValue(ctx, DBCONTEXTKEY, tx)
		return callback(ctx)
	})
}
